//--------- 整数部分 < 1  [0,240)  Maht.ceil( count / 240 * 60 )
// 1 / 240 = 0.004166666666666667  * 60 = 0.24
// 2 / 240 = 0.008333333333333333  * 60 = 0.48
// 3 / 204 = 0.0125  * 60 = 0.75
// 5 / 240 = 0.020833333333333332 * 60 = 1.2
// 10 / 240  = 0.0416 * 60 = 2.4
//  100 / 240 =  0.416  --- 24.6
//  200 / 240  --- 49.2
// 239 / 240 = 0.9958333333333333   * 60 = 59.4
//

// -------- 10 > 整数部分  >= 1  [240,2390)    Maht.ceil( count / 240 * 6 )
// 240 / 240 = 1 ---  1 *6
// 900 / 240 = 3.75  --  22.5
// 999 / 240 = 4.1625 --   24.9
// 1000 / 240 = 4.16 -- 24.9
// 2390 / 240 = 9.958333333333334 -- 59.7

// -------- 整数部分 >= 10  Math.min(240,(30 + 整数部分))
// 2400 / 240 = 10
// 5000 / 240  = 20.833333333333332
// 10000 / 240 = 41.6

/**
 *
 *
 * @param {number} diffCount 差值
 * @param {number} [totalSeconds=240] 总时间
 * @returns {number}
 * @description  一个整数 计算随机分成的份数
 */
function getParts(diffCount: number, totalSeconds = 240): number {
  let part
  const weight = diffCount / totalSeconds //比重
  const weightIntegerPart = Math.floor(weight) //比重整数部分
  if (weightIntegerPart < 1) {
    part = Math.ceil((diffCount / totalSeconds) * 60) //向上取整
  } else if (weightIntegerPart >= 1 && weightIntegerPart < 10) {
    part = Math.ceil((diffCount / totalSeconds) * 6)
  } else {
    part = Math.min(totalSeconds, 30 + weightIntegerPart)
  }

  //暂定不超过30
  part = Math.min(30, part)

  return part
}

// 如何把一个数拆成很固定个数的随机数，总和为这个数本身
// 微信分红包算法，达到较为平均的随机分配。

//注： ~~ 对于正数，它向下取整；对于负数，向上取整 , ~~-4.321 === -4 , Math.floor(-4.321) === -5

/**
 *
 *
 * @export
 * @param {number} num 目标值
 * @param {number} times 要分成功的份数
 * @returns {number[]}
 */
function getRandomNumGroup(num: number, times: number): number[] {
  const res = []
  if (times === 1) {
    res.push(num)
    return res
  }
  const max = (num / times) * 2
  const current = 1 + ~~(Math.random() * max - 1) //这个取整并保证最小值为1
  res.push(current)
  return res.concat(getRandomNumGroup(num - current, --times))
}

/**
 *
 *
 * @export
 * @param {number} startCount
 * @param {number} endCount
 * @param {number} [totalSeconds=240]
 * @returns {{ countGroup: number[], timeGroup: number[] }}
 */
export function generateCountArray(
  startCount: number,
  endCount: number,
  totalSeconds = 240
): { countGroup: number[]; timeGroup: number[] } {
  const diffCount = Math.abs(endCount - startCount)
  if (diffCount === 0) {
    return {
      countGroup: [],
      timeGroup: []
    }
  }
  const part = getParts(diffCount, totalSeconds)
  const countGroup = getRandomNumGroup(diffCount, part)
  let timeGroup = getRandomNumGroup(totalSeconds, part)
  if (timeGroup.length === 1 && timeGroup[0] === 240) {
    // diffCount=== 1/2 part === 1
    timeGroup = [1]
  }
  return {
    countGroup,
    timeGroup
  }
}

/**
 *
 *
 * @export
 * @param {number} count
 * @param {string} imgPath
 * @description 生成组织marker值数量
 */
export function renderMakerItemDom(
  count: number,
  imgPath: string,
  width: number
): HTMLDivElement {
  let pTop = 0
  switch (width) {
    case 20:
      pTop = 6
      break
    case 28:
      pTop = 3
      break
    case 39:
      pTop = 0
      break
    default:
      pTop = 0
  }

  const markerItemDom = document.createElement('div')
  markerItemDom.style.cssText = `width:${width}px;height:47px;position:relative;`
  const itemImg = document.createElement('img')
  itemImg.style.cssText = `position: absolute;left:0px;top:0px;width:${width}px;height:47px;overflow:hidden;z-index:1`
  itemImg.setAttribute('src', imgPath)
  const itemP = document.createElement('p')
  itemP.style.cssText = `position: absolute;left:0px;top:${pTop}px;width:${width}px;height:20px;font-size: 12px;color: #FFFFFF;line-height: 20px;text-align: center;font-family: ZQKHY;z-index:2`
  itemP.innerText = count.toString()
  markerItemDom.appendChild(itemImg)
  markerItemDom.appendChild(itemP)
  return markerItemDom
}
